package com.vancouver.knows.ui.fragment.homePage;


import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.OnClick;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.pangu.ui.browser.LoadableList;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.bean.WeatherInfo;
import com.vancouver.knows.ui.activity.TutorialActivity;
import com.vancouver.knows.ui.activity.search.NewSearchActivity;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.ui.view.compactcalendarview.AnimatorListener;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.business.Constants;

public class HomeFragment extends BaseFragment implements HomepageAddHeadView.HomepageListener, LoadableList.LoadableListScrollListener {

	private Context mContext;

	@Bind(R.id.homepage_layout)
	LinearLayout mLinearLayout;
	@Bind(R.id.weather_icon)
	ImageView mWeatherIcon;
	@Bind(R.id.weather_temp)
	TextView mWeatherTemp;
	@Bind(R.id.toolbar)
	Toolbar mToolbar;

	private HomepagePullRefreshView mController;

	public static HomeFragment newInstance() {
		return new HomeFragment();
	}

	@Override
	protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContext = getActivity();

		//启动 滑动首页
		showTutorialActivity();

		return inflater.inflate(R.layout.activity_homepage, container, false);
	}

	private void showTutorialActivity() {
		if (!imContext.getSharedPrefManager().hasSeenWalkThrough(BSharedPrefManager.WALKTHOUGH_1)) {
			Intent tutorialIntent = new Intent(getActivity(), TutorialActivity.class);
			tutorialIntent.putExtra(TutorialActivity.TUTORIAL_EXTRA, BSharedPrefManager.WALKTHOUGH_1);
			startActivity(tutorialIntent);
		}
	}

	@Override
	public void onResume() {
		super.onResume();

		Tracker t = ((MainApplication) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_HOME_FRAGMENT);
		t.send(new HitBuilders.ScreenViewBuilder().build());

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void bindView(View rootView) {
		setViews();
	}

	private void loadView() {
		mController = new HomepagePullRefreshView(mContext);
		((HomepagePullRefreshView) mController).loadData(this, getActivity());
		mLinearLayout.addView(mController);
		mController.setScrollListener(this);
	}

	private void setViews() {
		loadView();
	}

	@Override
	public void subHandleMessage(Message msg) {
		switch (msg.what) {
		case Constants.BUSINESS_STATUS_LOGIN_SUCCESS:
		case Constants.BUSINESS_STATUS_LOGIN_OVERTIME:
			if (mController != null) {
				mController.updateHomePage();
			}
			break;

		case Constants.BUSINESS_STATUS_SHOW_SPLASH_AD:
			//显示开屏广告
			boolean walkThough_1 = imContext.getSharedPrefManager().hasSeenWalkThrough(BSharedPrefManager.WALKTHOUGH_1);
			if (walkThough_1) {
				if (onSplashAdLiseners != null) {
					onSplashAdLiseners.showSplashAd();
				}
			}

			break;
		}

	}

	public void setSplashAdLiseners(OnSplashAdListeners listener) {
		this.onSplashAdLiseners = listener;
	}

	public void clearOnSplashAdLiseners() {
		this.onSplashAdLiseners = null;
	}

	private static OnSplashAdListeners onSplashAdLiseners = null;
	public interface OnSplashAdListeners {
		void showSplashAd();
	}

	@OnClick(R.id.search_icon)
	public void onSearchIconClick() {
		Intent searchIntent = new Intent(mContext, NewSearchActivity.class);
		startActivity(searchIntent);
	}

	public void scrollUp() {
		if (mController != null) {
			mController.scrollUp();
		}
	}

	@Override
	public void onWeatherInfoReceived(WeatherInfo weatherInfo) {
		if (weatherInfo != null) {
			Picasso.with(mContext).load(weatherInfo.getIcon()).fit().into(mWeatherIcon);
			mWeatherTemp.setText(weatherInfo.getTemperature());
		}
	}

	@Override
	public void onScrollUp() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.VISIBLE);
			}
		});
		viewPropertyAnimator.translationY(0).setInterpolator(new AccelerateInterpolator()).start();
	}

	@Override
	public void onScrollDown() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.GONE);
			}
		});
		viewPropertyAnimator.translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
	}
}
